package com.blog1.common.interceptor;

import com.blog1.common.constants.Constants;
import com.blog1.common.util.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头中的token
        String token = request.getHeader(Constants.USER_TOKEN_HEADER_KEY);
        log.info("从header中获取token:{}"+token);
        if(token==null){
       response.setStatus(401);
            return false;
        }

        //解析token
        Claims claims= JwtUtils.parseToken(token);
        if(claims==null){
            //验证token
            response.setStatus(401);
            return false;
        }
        return true;
    }


}
